﻿using System;
using System.Collections.Specialized;
using System.Text;
using System.Web.Mvc;
using ABC.Core.Configuration;
using ABC.Core.Providers;
using RosService.Files;

namespace ABC.Core.Extentions
{
	public static class UrlExtention
	{
		public static string VirtualImageUrl(this UrlHelper urlHelper, string file)
		{
			var virtualImagePath = IoC.Resolve<IApplicationSettingsProvider>().VirtualImagePath;
			return string.Concat(virtualImagePath, file);
		}

		public static string VirtualScriptUrl(this UrlHelper urlHelper, string file)
		{
			var virtualImagePath = IoC.Resolve<IApplicationSettingsProvider>().VirtualScriptPath;
			return string.Concat(virtualImagePath, file);
		}

		public static string VirtualStyleUrl(this UrlHelper urlHelper, string file)
		{
			var virtualImagePath = IoC.Resolve<IApplicationSettingsProvider>().VirtualStylePath;
			return string.Concat(virtualImagePath, file);
		}

		public static string Image(this UrlHelper urlHelper, string imageId, int width, int height)
		{
			if (string.IsNullOrEmpty(imageId))
			{
				return urlHelper.VirtualImageUrl("userpic.jpg");
			}

			return urlHelper.Content(GetUrlResize(imageId, width, height));
		}

        public static string Image(this UrlHelper urlHelper, string imageId)
        {
            if (string.IsNullOrEmpty(imageId))
            {
                return urlHelper.VirtualImageUrl("userpic.jpg");
            }

            return urlHelper.Content(GetUrlFull(imageId));
        }

		public static string Gift(this UrlHelper urlHelper, string gift)
		{
			if (string.IsNullOrEmpty(gift))
			{
				return urlHelper.VirtualImageUrl("userpic.jpg");
			}

			return urlHelper.Content(string.Concat(@"~/images/gifts/", gift));
		}

		public static string Avatar(this UrlHelper urlHelper, string avatarId, int size)
		{
			if (string.IsNullOrEmpty(avatarId))
			{
				return urlHelper.VirtualImageUrl("userpic.jpg");
			}

			return urlHelper.Content(GetUrlThumbnail(avatarId, size));
		}

		private static string GetUrlThumbnail(string avatarId, int size)
		{
			var param = new NameValueCollection();
			param["id_node"] = avatarId;
			param["width"] = size.ToString();
			param["height"] = size.ToString();
			param["imagetype"] = ImageType.Thumbnail.ToString();

			return string.Format("~/image.ashx?q={0}", EncodingQueryParam(param));
		}

		private static string GetUrlResize(string imageId, int width, int height)
		{
			var param = new NameValueCollection();
			param["id_node"] = imageId;
			param["width"] = width.ToString();
			param["height"] = height.ToString();
			param["imagetype"] = ImageType.Resize.ToString();

			return string.Format("~/image.ashx?q={0}", EncodingQueryParam(param));
		}

        private static string GetUrlFull(string imageId)
        {
            var param = new NameValueCollection();
            param["id_node"] = imageId;
            param["width"] = (-1).ToString();
            param["height"] = (-1).ToString();
            param["imagetype"] = ImageType.Full.ToString();

            return string.Format("~/image.ashx?q={0}", EncodingQueryParam(param));
        }

		private static string EncodingQueryParam(NameValueCollection paramList)
		{
			var query = "";
			for (var i = 0; i < paramList.Count; i++)
			{
				query += string.Format("{0}{1}={2}", (i > 0 ? "&" : ""), paramList.Keys[i], paramList[i]);
			}
			return Convert.ToBase64String(Encoding.UTF8.GetBytes(query));
		}

        private static string GetUrlYandexThumbnail(string dir, int width, int heigth)
        {
            var param = new NameValueCollection();
            param["dir"] = dir;
            param["width"] = width.ToString();
            param["height"] = heigth.ToString();
            return string.Format("~/yaimage.ashx?q={0}", EncodingQueryParam(param));
        }

        public static string Thumbnail(this UrlHelper urlHelper, string dir, int width, int heigth)
        {
            if (string.IsNullOrEmpty(dir))
            {
                return urlHelper.VirtualImageUrl("userpic.jpg");
            }
            return urlHelper.Content(GetUrlYandexThumbnail(dir, width, heigth));
        }
	}
}